﻿// WF插件封裝 creat by alan 2013.04.25
(function($)
{
     //String 擴展
    $.extend(String.prototype, 
    {
        isPositiveInteger: function() {
            return (new RegExp(/^[1-9]\d*$/).test(this))
        },
        isInteger: function() {
            return (new RegExp(/^\d+$/).test(this))
        },
        isNumber: function(value, element) {
            return (new RegExp(/^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/).test(this))
        },
        trim: function() {
            return this.replace(/(^\s*)|(\s*$)|\r|\n/g, "")
        },
        replaceAll: function(os, ns) {
            return this.replace(new RegExp(os, "gm"), ns)
        },
        isValidPwd: function() {
            return (new RegExp(/^([_]|[a-zA-Z0-9]){6,32}$/).test(this))
        },
        isValidMail: function() {
            return (new RegExp(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/).test(this.trim()))
        },
        isPhone: function() {
            return (new RegExp(/(^([0-9]{3,4}[-])?\d{3,8}(-\d{1,6})?$)|(^\([0-9]{3,4}\)\d{3,8}(\(\d{1,6}\))?$)|(^\d{3,8}$)/).test(this))
        },
        isUrl: function() {
            return (new RegExp(/^[a-zA-z]+:\/\/([a-zA-Z0-9\-\.]+)([-\w .\/?%&=:]*)$/).test(this))
        }
    });
    $.fn.extend({
        layoutH: function($refBox) 
        {
			return "";
		},
		hoverClass: function(className) {
			var _className = className || "hover";
			return this.each(function() {
				$(this).hover(function() {
					$(this).addClass(_className);
				},
				function() {
					$(this).removeClass(_className);
				});
			});
		},
		focusClass: function(className) 
		{
			var _className = className || "textInputFocus";
			return this.each(function() {
				$(this).focus(function() {
					$(this).addClass(_className);
				}).blur(function() {
					$(this).removeClass(_className);
				});
			});
		},
		//創建grid
		grid:function(options) 
		{
		    $this = $(this);
		    var op = $.extend({data: "",
                       datatype: "local",      
                       autowidth: true,
                       height: 400,
                       rowNum: 50,
                       rowList: [10, 15, 20, 50, 100, 200],
                       multiselect: true,
                       multiboxonly: true,   
                       multiselectonrow: false, 
                       loadonce: true,
                       viewrecords: true,
                       pagerpos:"left",
                       rownumbers: true,           
                       toppager: true,
                       forceFit: true,
                       gridview: true,                              //大型數據載入 >=100行，設置為true提升速度5～6倍，不能使用treegrid subgrid afterinsertrow
                       shrinkToFit: true,
                       altRows: true,                               //設定斑馬紋效果
                       altclass: "ui-widget-altclass"   	        //設定斑馬紋樣式                       
                       }, options);
            $this.jqGrid(op);
            return $this;	    
		},
		//數據綁定 json
		gridBind:function(data)
		{
	        $(this).jqGrid('clearGridData');
            $(this).jqGrid('setGridParam', {data: data}).trigger("reloadGrid");
		},
		//日期控件
		date:function(option)
        {        
            $(this).datetimepicker(
            {
                format: "yyyy/mm/dd",
                weekStart: 1,
                language:　"zh-TW",
                todayBtn:  1,
		        autoclose: 1,
		        todayHighlight: 1,
		        startView: 2,
		        forceParse: 0,
                showMeridian: 1,
                startDate: "2008/01/01",
                endDate:　"2015/12/31",
                minView: 2,
                maxView: 4,
                keyboardNavigation: true,
                pickerPosition: "bottom-left",  
                forceParse: true           
            });   
            return $(this);
        },
        //自動combox創建
		combox :function(options)
	    {
	        var op = $.extend({name:""}, options);
	        if($(this).length) 
	        {
                $(this).select2(
                {
                    allowClear: true,
                    placeholder: op.name                    
                });
            }
        },
        //下拉列表資料綁定
        selectBind:function(data)
        {
            $this = $(this);
            if($(this).length) 
            {
                if(null!=data)
                {
                    $(data).each(function(i,v)
                    {
                        $this.append("<option value='"+v.text+"'>"+v.value+"</option>");
                    });
                }
            }
            return $this;
        },
        //List創建
        list:function(options)
        {
            var op = $.extend({height:"300",name1:"所有選擇項目",name2:"已選擇項目"}, options);
            if($(this).length) 
	        {
	            $(this).multiSelect(
	            {
	                height:op.height,
                    selectableHeader: "<div class='multipleselect-header'>"+op.name1+"</div>",
                    selectionHeader: "<div class='multipleselect-header'>"+op.name2+"</div>"    
                });
            }
        },
        //List資料取值
        listGetVal:function()
        {
            var data = new Array();
            var item = {};
            if($(this).length) 
	        {
                $(this).find("option:selected").each(function(i,v)
                {
                    item.text = $(v).text();
                    item.value = $(v).val();
                    data.push(item);
                });
            }
            return data;
        }
     });
})(jQuery);